<script setup>
import { computed } from 'vue';

const props = defineProps({
  list: Array,
  default: () => []
})
// console.log('props.list -----> ', props.list);

// 定义好方法，在input改变时候触发
const emit = defineEmits(['change', 'del', 'changeAll'])

// 是否全选
const isCheckAll = computed(
  () => {
    // some 数组内有一项符合条件会返回 true
    // every 数组内每一项都符合条件会返回 true
    // 但是every，对于空数组，也会返回true，所以我们需要做兼容处理
    return props.list.length > 0 && props.list.every(v => v.done)
  }
)

// 全选/反选
const changeAll = (e) => {
  console.log('e.target -----> ', e.target.checked);
  emit('changeAll', e.target.checked)
}
</script>

<template>
  <section class="main">
    <input id="toggle-all" class="toggle-all" type="checkbox" :checked="isCheckAll" @change="changeAll" />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <li :class="{ completed: item.done }" v-for="item in list" :key="item.id">
        <div class="view">
          <input class="toggle" type="checkbox" :checked="item.done" @change="emit('change',item.id)" />
          <label>{{item.name}}</label>
          <button class="destroy" @click="emit('del',item.id)"></button>
        </div>
        <input class="edit" value="Create a TodoMVC template" />
      </li>
    </ul>
  </section>
</template>

<style lang="less" scoped>

</style>
